*Table 14.3.6.1;
*Summary of ECG Parameters;
*(Clinical Safety Set);

*Macro to obtain data and create a Report;

%macro prnt (program=, idisname=, disname=, ds_list=, popt=, title=);  

%get_data(lst=adeg,pre=adam,supp =N,lib=adam);

proc freq data =adamadeg ;
   tables paramcd*param/ list missing nocum nopercent;
run;

 /*data tempor(keep=subjid usubjid param paramcd paramn aval avalc avisit avisitn atpt atptn period periodn trtp trtpn EGPREC );*/
/*  set adamadeg;*/
/*      if saffl="Y" and aval ne . and paramcd ne "INTP"and visityp="Scheduled";*/
/*run;*/

data tempor(keep=subjid usubjid param paramcd paramn aval avalc avisit avisitn VISITYP adt atm ADTM trtp trtpn ANL01FL);
  set adamadeg;
    if saffl="Y" and aval ne . and paramcd ne "INTP" ;
if (avisit=:"Scr") and VISITYP=:"Uns" then delete;
if index(VISITYP,"Unscheduled") then delete;
run;


proc sort data=tempor;
by USUBJID paramcd PARAMN AVISITN ADTM;
run;

%macro pre(pr,m, par,period,parr);
	
proc sql;
create table &pr.n as select count(distinct usubjid) as N, &m as ord, avisit, avisitn, /*atpt,*/ "&par" as parameter length 24, "&parr" as unit length 50
	from tempor where paramcd="&par" group by &period, avisitn/*, atpt*/;
create table &pr.mean as select mean(aval) as mean format 6.0, &m as ord, avisit, avisitn, /*atpt,*/ "&par" as parameter length 24, "&parr" as unit length 50 
	from tempor where paramcd="&par" group by &period, avisitn/*, atpt*/;
create table &pr.std as select std(aval) as sd format 6.1, &m as ord, avisit, avisitn,/*atpt,*/ "&par" as parameter length 24, "&parr" as unit length 50 
	from tempor where paramcd="&par" group by &period, avisitn/*atpt,*/;
create table &pr.med as select median(aval) as median format 6.1, &m as ord, avisit, avisitn, /*atpt,*/ "&par" as parameter length 24, "&parr" as unit length 50 
	from tempor where paramcd="&par" group by &period, avisitn/*atpt,*/;
create table &pr.min as select min(aval) as min, &m as ord, avisit, avisitn,/*atpt,*/ "&par" as parameter length 24, "&parr" as unit length 50 
	from tempor where paramcd="&par" group by &period, avisitn/*atpt,*/;
create table &pr.max as select max(aval) as max, &m as ord, avisit, avisitn,/*atpt,*/"&par" as parameter length 24, "&parr" as unit length 50 
	from tempor where paramcd="&par" group by &period, avisitn/*atpt,*/;
quit;

data &pr.1(drop=n);
set &pr.n;
length col col0 $20.;
col="n";
col0=strip(put(n,best.));
b=1;
run;

data &pr.2(drop=mean sd sdc);
merge &pr.mean &pr.std;
by &period/*atpt*/;
length col col0 $20.;
if sd ne . then sdc=strip(put(sd,6.2));
if sd=. then sdc="--";
col0=strip(put(mean,6.1))||" ("||strip(sdc)||")";
/*col0=strip(put(mean,6.0))||" ("||strip(put(sd,6.1))||")";*/
col="Mean (SD)";
b=2;
run;

data &pr.3(drop= median);
set &pr.med;
length col col0 $20.;
col="Median";
col0=strip(put(median,6.1));
b=3;
run;

data &pr.4(drop=min max );
merge &pr.min &pr.max;
by &period /*atpt*/;
length col col0 $20.;
col="Min, Max";
col0=strip(put(min,best.))||", "||strip(put(max,best.));
b=4;
run;

data &pr;
set &pr.1 &pr.2 &pr.3 &pr.4;
run;

proc sort data=&pr; by avisitn &period /*atpt,*/ord; run;
%mend pre;


%pre(egvrmn,1,%str(EGVRMN),avisit,%str(ECG Mean Ventricular Rate (beats/min)));
%pre(prag,2,%str(PRAG),avisit,%str(PR Interval, Aggregate (msec)));
%pre(qrsag,3,%str(QRSAG),avisit,%str(QRS Duration, Aggregate (msec)));
%pre(qtag,4,%str(QTAG),avisit,%str(QT Interval, Aggregate (msec)));
%pre(qtcbag,5,%str(QTCBAG),avisit,%str(QTcB Interval, Aggregate (msec)));
%pre(qtcfag,6,%str(QTCFAG),avisit,%str(QTcF Interval, Aggregate (msec)));

data all; 
set egvrmn prag qrsag qtag qtcbag qtcfag ;
/*if atpt="PREDOSE" then atptn=0;*/
/*else if atpt="3.00 HOURS" then atptn=3;*/
/*else atptn=.;*/
*avisit=tranwrd(avisit,"Screening Day -28 to -1","Screening");
*avisit=tranwrd(avisit,"Post Study Day 21/Early Termination","Post Study Day 21");
val="Value";
run;

proc sort data=all; by ord avisitn /*atptn*/ b; run;

*Total number of patients by avisitn to display in a header;

proc sql noprint;
select count(distinct usubjid) into :n from adamadeg;
quit;

%let n=%sysfunc(compress(&n));
%put &n;

%page_cut(in=all,sort=ord unit avisitn avisit /*atptn*/ b,nocut=avisit,newpage=unit,pagesize=22,skip=unit,
	order=unit avisit /*atpt*/ col col0, columns=unit  avisit /*atpt*/ col col0, width=, defln=1,nlsym=\par,out=report);

data report;
	set report;

	if page in (1,2) then
		page_=1;

	if page in (3,4) then
		page_=2;

	if page in (5,6) then
		page_=3;
run;


*******************************************************************************************************
Printing using proc report
******************************************************************************************************;

title3 " ";
title4 "Table &disname (Continued)";
%footer1(foot1=1, ds_list=&ds_list.,program=&program.);

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&idisname..EG.SUM.rtf";

%open_rtf;
%empty_ds_fix(report);

title5 "Summary of ECG Parameters";
title6 "(Safety Population)";

*footnote3 j=left "Note: When tests are repeated for a visit, the value closest to dosing time is used.";
 
proc report data=report nowd split='~' missing;
 column page_ ord unit parameter avisitn avisit val b col col0;

 define page_       / " " order order=internal noprint;
 define ord     / " " order order=internal noprint;

 define unit / "Parameter (Unit)~ " order order=internal style(column)=[cellwidth=3in just=L] style(header)=[just=Left];
 define parameter / " " order order=internal noprint;

 define avisitn     / " " order order=internal noprint;
 define avisit / "Visit~ " order order=internal style(column)=[cellwidth=2in just=L] style(header)=[just=Left];
 *define atptn     / " " order order=internal noprint;
 *define atpt / "Time Point" order order=internal style(column)=[cellwidth=1.2in just=L] style(header)=[just=Left];

 define val / " " order order=internal style(column)=[cellwidth=1.4in just=L] style(header)=[just=Left];

 define b     / " " order order=internal noprint;

 define col / " " order order=internal style(column)=[cellwidth=1.5in just=left] style(header)=[just=c];
 define col0 / "Overall~ (N=&n)" order order=internal style(column)=[cellwidth=1.9in just=C] style(header)=[just=c];

break after page_/page;
 
   compute before unit;
    line " ";
   endcomp;

   compute after avisit;
    line " ";
   endcomp;
run;
%close_rtf;
%arrange_rtf;
%mend prnt;

%prnt(program=T14_03_06_01_EG_SUM, idisname=14.3.6.1, disname=14.3.6.1, ds_list=ADEG);

%clrw;